﻿<!DOCTYPE html>
<!--  window-icon 弹出框图 例: window-icon="http://www.qt6.com/favicon.ico"    -->
<html lang="en" window-frame="solid" window-icon="./view/icon/server-favicon.ico"></html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加定时数据库备份任务</title>
    <link rel="stylesheet" type="text/css" href="./view/css/index.css">
    <style>
        .backups input[type=checkbox],.backups span{
            line-height: 38px;
        }
        #box {
            width: 645px;
            height: 500px;
        }
        .head {
            height: 30px;
            border-bottom: 1px solid #fff;
        }
        #datastart {
            width: 400px;
        }
        #datastart select{
            width:55px;
        }
        #datastart select option{
            width:55px;
        }
        #datastart ul li {
            float: left;
        }
    </style>
</head>
<body role="window-caption">
    <div id="box">
        <!-- 模态框 -->
        <div class="addtaskmodetop skinbg" style="background-image: url(./view/img/boxbg.png);">
                <div class="title fl" >
                    <img src="./view/img/logo_st.png" alt="" class="fl">
                   添加任务
                </div>
                <button id="closewin" class="closewinicon topiconBg addmodeclosebtn"  style="background-image: url(./view/img/topicon.png);"></button>
        </div>

        <div class="addmode addtaskmode addtask layui-anim layui-anim-scale">
            <div class="layui-form" action="" style="margin-top:20px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item period">
                        <label class="layui-form-label"> 开始时间：</label>
                        <div id="datastart" class="layui-input-block">
                           <ul>
                               <li id="year"></li>
                               <li id="month"></li>
                               <li id="day"></li>
                               <li id="hour"></li>
                               <li id="minute"></li>
                           </ul>
                        </div>
                    </div>
                <div class="layui-form-item period">
                    <label class="layui-form-label">周期：</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input perioddom" name="interval">
                        <select id="sel" name="unit" style="float: right;">
                            <option value="m"selected>分</option>
                            <option value="h">时</option>
                            <option value="d">天</option>
                            <option value="z">周</option>
                            <option value="M">月</option>
                        </select>
                        <div class="verifyint">请输入整数</div>
                    </div>
                </div>
                <div class="layui-form-item period" >
                    <label class="layui-form-label" >数据库类型：</label>
                    <div class="layui-input-block">
                        <select id="sqltypedata" name="sqltype" style="width:400px;">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item period">
                    <label class="layui-form-label ">备份数据库：</label>
                    <div id="checkboxid" class="layui-input-block backups">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" >
                        <button id="addtasks" class="layui-btn" >立即提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/tiscript" src="./view/tiscript/index.tis"></script>
    <script type="text/tiscript" src="./view/tiscript/public.tis"></script>
    <script type="text/tiscript">
        var date = new Date()
        self.ready = function() {

              var sqlconfiglist = view.parameters["newin"].sqlconfiglist()

            // 数据库 类型 库
         
            var optionhtml = ""
            var selecteds = ""

            for(var i = 0; i < sqlconfiglist.length; i++){
                    if(i == 0){
                        getviewsql(sqlconfiglist[i].category,sqlconfiglist[i].version)
                        selecteds = "selected"
                    }else{
                        selecteds = "" 
                    }
                    var versionval = ""
                    if(sqlconfiglist[i].version){
                        versionval = "版本号: "+sqlconfiglist[i].version
                    }else{
                        versionval = ""
                    }
                    optionhtml += `
                        <option value="`+sqlconfiglist[i].category+`_`+sqlconfiglist[i].version+`" `+selecteds+`><span style="float: left;margin-left: 30px;">`+sqlconfiglist[i].category+`</span><span style="float: right;margin-right: 30px;">`+ versionval +`</span></option>
                    `
            }

            $(#sqltypedata).html = optionhtml
        
            $(select[name=sqltype]).on("change",function(){
                var value = this.value
                var category = value.split("_")[0]
                var version = value.split("_")[1]
                getviewsql(category,version)                
            })


            //获取秘钥信息
            var rsalist = view.parameters["newin"].getrsainfo();
            
            if (!rsalist){
                     var argument = {
                            "newin":view.parameters["newin"],
                            "type":"systips",
                            "title":"系统提示",
                            "text":"请先生成或上传秘钥",
                            "buttons":[{id:"yeskey",text:"确认"}],
                            "url":"./view/privateSecret.html"                            
                        }

                        view.dialog({
                            //加载到对话框中的URL，可加载本地文件
                            url: "./view/popup.html",
                            //对话框的x，y坐标
                            x: 0,
                            y: 0,
                            //窗口的宽度，高度
                            width: 272,
                            height: 170,
                            //是否是客户区，如果为真，那么x，y，width，height作为客户区坐标，而不是窗口
                            client: false,
                            //对话框 参数
                            parameters: argument,
                            //对话框标题
                            caption: "新对话框",
                            //相对于屏幕窗口对齐
                            alignment: 1,
                        });
                        view.close();
                        return;
              }


           

            // 判断周期
            var interval = /^-?[1-9]+[0-9]*$/
            $(input[name=interval]).on("change",function(){
                var val = $(input[name=interval]).value
                if(val.length > 0){
                    var reg = interval.test(val)
                    if(!reg){
                        $(.verifyint).style["display"] = "block"
                    }else{
                        $(.verifyint).style["display"] = "none"
                    }
                }else{
                    $(.verifyint).style["display"] = "none"
                }
                
            })
            
            // 时间设置
            var yearval = date.year ,monthval = date.month,dayval= date.day,hourval = date.hour,minuteval= date.minute
            
            //年
            setyear(date.year) 
            // 月
            setmonth(date.month,date.year) 
            // 日
            setday(date.month) 
            // 小时
            sethour()
            // 分
            setminute()
          
            // 所选 数据库名
            var mysqlstr = ""
            // 验证时间
            var re = /^(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d$/;
            var elUnit = "m"
            // 默认数据库名
            var sqlname = ""

            $(#addtasks).on("click", function() {
                var mysqlnamebox = $(select[name=mysqlname]).$$(option);
                var yearbox = $(select[name=year]).$$(option);
                var monthbox = $(select[name=month]).$$(option);
                var daybox = $(select[name=day]).$$(option);
                var hourbox = $(select[name=hour]).$$(option);
                var minutebox = $(select[name=minute]).$$(option);
                var unitbox = $(select[name=unit]).$$(option);
                // 数据库
                var sqltypebox = $(select[name=sqltype]).$$(option);
                var sqltype = getSelectvalue(sqltypebox)
                // 获取 数据库 类型
                var sqlcategory = sqltype.split("_")[0]
                // 获取 数据库 版本 
                var sqlversion = sqltype.split("_")[1]

                sqlname = getSelectvalue(mysqlnamebox)
                yearval = getSelectvalue(yearbox)
                monthval = getSelectvalue(monthbox)
                dayval = getSelectvalue(daybox)
                hourval = getSelectvalue(hourbox)
                minuteval = getSelectvalue(minutebox)
                elUnit = getSelectvalue(unitbox)

                var elName = $(input[name=name]); 
                var elInterval = $(input[name=interval]); 
                if(!elName.value || !elInterval.value){
                    view.msgbox(#warning, "请填写正确信息");
                }else if(sqlname == ""){
                    view.msgbox(#warning, "请选择备份数据库");
                }else{
                    var num = 1
                    if(monthval < 10){
                        monthval = "0" + monthval
                    }else if(dayval < 10 ){
                        dayval = "0" + dayval
                    }else if(hourval < 10 ){
                        hourval = "0" + hourval
                    }else if(minuteval < 10 ){
                        minuteval = "0" + minuteval
                    }
                    var newdateval = new Date()

                    var newdate = yearval +"-"+monthval+"-"+dayval+" "+hourval+":"+minuteval
                    var deal = Date.parse(newdate)
                    var startDate = deal/1000
                    var newdeal = newdateval.valueOf()
                    var newdealDate = newdeal/1000
                    // 所选时间 和当前时间 比较
                   var Interval = elInterval.value
                    if(startDate > newdealDate){
                        startDate = startDate.toInteger()
                        var elInter = ""
                        if (elUnit == "d") {
                            elInter = Interval.toInteger()*24 + "h";
                        }else if (elUnit == "z") {
                             elInter = Interval.toInteger()*24*7+ "h";
                        }else if (elUnit == "m") {
                             elInter = Interval.toInteger() + "m";
                        }else if (elUnit == "h") {
                             elInter = Interval.toInteger() + "h";
                        }else if (elUnit == "M") {
                             elInter = Interval.toInteger() + "M";
                        }
                        var json = {
                            name:elName.value, // 名称
                            starttime:startDate,  // 开始日期
                            interval:elInter, // 周期
                            repeatcount:0,  //重复次数
                            dbs:sqlname, // 备份数据库
                            category:sqlcategory, // 数据库类型
                            version:sqlversion // 数据库版本号
                        }

                       var addtaskres = view.parameters["newin"].addtask(json)
                        if (addtaskres.success) {
                            view.close();
                            view.parameters["success"]("添加成功",view.parameters["fun"])
                        }else{
                            view.parameters["error"](addtaskres.msg)
                        }
                       
                    }else{
                        view.msgbox(#warning, "请将开始日期大于当前时间");
                    }
                    
                }
            });

         
            $(#state).on("click", function() {
                //关闭
                view.close();
            });
            //视图窗口移动时
            view.on("moving", function(movingParams) {
                view.dump("移动改变", movingParams.x, movingParams.y, movingParams.width, movingParams.height);
            });
        }
        // 数据库
        function getviewsql(category,version){
         //获取mysql数据库列表
                var dblist = view.parameters["newin"].getsqllist(category,version);
                var mysql = ""
                var selected = ""
                if (dblist.success) {
                    var dblistarr = dblist.msg.split(",");
                    $(#checkboxid).html = "";
                    for (var (key,val) in dblistarr){
                        if(key == 0){
                            selected = "selected"
                        }else{
                            selected = ""
                        }
                        mysql += `<option value="`+val+`" `+selected+` >`+val+`</option>`
                    }
                    $(#checkboxid).html =   
                            `<select id="sqlsel" name="mysqlname" style="width:400px;">`+mysql+`</select>`;
                }else{
                    var argument = {
                        "newin":view.parameters["newin"],
                        "type":"systips",
                        "title":"系统提示",
                        "text":dblist.msg,
                        "buttons":[{id:"yeskey",text:"确认"}],  
                    }

                    view.dialog({
                        //加载到对话框中的URL，可加载本地文件
                        url: "./view/popup.html",
                        //对话框的x，y坐标
                        x: 0,
                        y: 0,
                        //窗口的宽度，高度
                        width: 272,
                        height: 170,
                        //是否是客户区，如果为真，那么x，y，width，height作为客户区坐标，而不是窗口
                        client: false,
                        //对话框 参数
                        parameters: argument,
                        //对话框标题
                        caption: "新对话框",
                        //相对于屏幕窗口对齐
                        alignment: 1,
                    });
                    view.close();
                    return;
            }
        }

        // 年
        function setyear(yearval){
            var year = ""
            for (var i = toInteger(date.year)  ; i < toInteger(date.year)+50 ;  i++){
                var selected = "selected"
                if(date.year == i){
                    selected = "selected"
                }else{
                    selected = ""
                }
                year += `<option value="`+i+`" `+selected+`>`+i+`</option>`
            }
            $(#year).html = `<select name="year"> 
                        `+year+`
                    </select>
                    <label>年:</label>`
        }
        // 月
        function setmonth(monthval,yearval){ 
            var monthsval = 1
            if(yearval > date.year){
                monthsval = 1
            }else if(yearval == date.year){
                monthsval = date.month
            }
            // 月
            var month = ""
            for (var i = 1 ; i < 13 ;  i++){
                var selected = "selected"
                if(monthval == i){
                    selected = "selected"
                }else{
                    selected = ""
                }
                month += `<option value="` + i + `" `+selected+` >` + i + `</option>`
            }
         
            $(#month).html = `<select name="month" >
                        `+month+`
                    </select>
                    <label>月:</label>`
        } 
        // 天
        function setday(monthval){
            var days = 31
            var date = new Date()
            if(monthval == 1 || monthval == 3 ||  monthval == 5 || monthval == 7 || monthval == 8 || monthval == 10 || monthval == 12){
                days = 31
            }else if(monthval == 4 || monthval == 6 ||  monthval == 9 || monthval == 11 ){
                days = 30
            }else if(monthval == 2 ){
                if(((date.year%400==0) || (date.year%100!=0)) && (date.year%4==0)){
                    days = 29
                }else{
                    days = 28
                }
            }
            var dayvals = 1
            if(monthval > date.month){
                dayvals = 1
            }else if(monthval == date.month){
                dayvals = date.day
            }
            var day = ""
            for (var i = 1 ; i < 31 ;  i++){
                var selected = "selected"
                if(date.day == i){
                    selected = "selected"
                }else{
                    selected = ""
                }
                day += `<option value="`+i+`" `+selected+`>`+i+`</option>`
            }

            $(#day).html = `<select name="day">
                            `+day+`  
                    </select>
                    <label>日:</label>`
        }
        // 小时 
        function sethour(){
            var hour = ""
            for (var i = 0 ; i < 24 ;  i++){
                var selected = "selected"
                if(date.hour == i){
                    selected = "selected"
                }else{
                    selected = ""
                }
                hour += `<option value="`+i+`" `+selected+`>`+i+`</option>`
            }
            $(#hour).html = `<select name="hour" >
                        `+hour+` 
                    </select>
                    <label>时:</label>`
        }

        // 分钟 
        function setminute(){
            var minute = ""
            for (var i = 0; i < 60 ;  i++){
                var selected = "selected"
                if(date.minute == i){
                    selected = "selected"
                }else{
                    selected = ""
                }
                minute += `<option value="`+i+`" `+selected+`>`+i+`</option>`
            }
            $(#minute).html = `<select stype="width:30px;" name="minute">
                            `+minute+`
                        </select>
                        <label>分</label>`
        }
       
            
     
           
    </script>
</body>
</html>